//
//  ArticalViewController.m
//  GiftNote
//
//  Created by lanou3g on 15/8/11.
//  Copyright (c) 2015年 GN. All rights reserved.
//

#import "ArticalViewController.h"
#import "MyCollectionViewCell.h"
#import "DetailsViewController.h"
#import "AFHTTPRequestOperationManager.h"
#import "INHeader.h"
#import "ArtiollerModel.h"
@interface ArticalViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
{
    UICollectionView *collectionView;
}
@property (nonatomic,retain) NSMutableArray *magerModelArr;
@end

@implementation ArticalViewController

- (NSMutableArray *)magerModelArr{
    if (!_magerModelArr) {
        self.magerModelArr = [NSMutableArray array];
    }
    return _magerModelArr;
}

- (void)createWithManger{
    AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManager manager];
    [manger GET:KSingleURL parameters:self success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSArray *arr = responseObject[@"data"][@"items"];
        for (NSDictionary *dic in arr) {
           // NSDictionary *dict = dic[@"data"];
            ArtiollerModel *artiollerModel = [[ArtiollerModel alloc] init];
            [artiollerModel setValuesForKeysWithDictionary:dic[@"data"]];
            [self.magerModelArr addObject:artiollerModel];
        }
        [collectionView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error==%@",error);
    }];
}
- (void)viewWillAppear:(BOOL)animated{
    
    self.navigationController.toolbarHidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.barTintColor = [UIColor orangeColor];
    //  改变下一层返回按钮的标题
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];
    [self.navigationItem setBackBarButtonItem:backItem];
    
    //  改变标题的颜色
    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                [UIColor whiteColor],
                                NSForegroundColorAttributeName, nil];
    [self.navigationController.navigationBar setTitleTextAttributes:attributes];
    
    
    self.navigationItem.title = @"单品";
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
=======
    [self createWithManger];
    self.navigationItem.title = @"单品";
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
>>>>>>> 6250a189afb58b704860e5e2903934a3bf9c3fe8
    collectionView.backgroundColor = [UIColor whiteColor];
    [collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [self.view addSubview:collectionView];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.magerModelArr.count;

}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    [cell setArtiollerModel:_magerModelArr[indexPath.row]];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake(180, 300);
}
//跳转到详情:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    DetailsViewController *detaisViewVc = [[DetailsViewController alloc] init];
    [detaisViewVc setDetailsModel:_magerModelArr[indexPath.row]];
    
    //隐藏tabbar
    detaisViewVc.hidesBottomBarWhenPushed = YES;
    
    [self.navigationController pushViewController:detaisViewVc animated:YES];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
